//
//  TextAnimationTool.m
//  ToolLibrary
//
//  Created by mac on 2019/5/2.
//  Copyright © 2019 杨洋. All rights reserved.
//

#import "TextAnimationTool.h"
#import <CoreText/CoreText.h>

static NSString *drawString;
static NSInteger i;
static NSInteger subIndex;

@interface TextAnimationTool ()<CAAnimationDelegate>
/** 是否超过一行 */
@property (nonatomic, assign) BOOL oneMoreText;
/** 需要绘制的文字数组 */
@property (nonatomic, strong) NSArray *textArr;
@property (nonatomic, assign) NSInteger stringIndex;
///** 一行能放几个文字 */
//@property (nonatomic, assign) NSInteger lineNumber;
/** 每行文字的宽度 */
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) NSInteger wordWidth;
@property (nonatomic, assign) NSInteger lineHeight;
/** 每行文字的宽度 */
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) UIColor *bgColor;
@property (nonatomic, strong) UIColor *fontColor;
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) UIView *mySuperView;
@property (nonatomic, assign) CGFloat fontHeight;


@property (nonatomic, strong) NSMutableArray *shapeLayers;

@end

@implementation TextAnimationTool

static TextAnimationTool *singletonInstance = nil;
//static dispatch_once_t onceToken;

#pragma mark - ARC

+ (instancetype)shareInstance{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singletonInstance = [[self alloc] init];
    });
    if (!singletonInstance) {
        singletonInstance = [[self alloc] init];
    }
    return singletonInstance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singletonInstance = [super allocWithZone:zone];
    });
    
    return singletonInstance;
}

- (id)copyWithZone:(struct _NSZone *)zone{
    
    return singletonInstance;
}

#pragma mark - MRC

#if __has_feature(objc_arc)

#else

- (instancetype)retain{
    
    return singletonInstance;
}

- (NSUInteger)retainCount{
    
    return 1;//此处也可以返回max
}

- (oneway void)release{
    
}

- (instancetype)autorelease{
    
    return singletonInstance;
}

#endif

#pragma mark - 文字绘制(字符串)(使用默认属性)
-(void)drawAnimationWithText:(NSString *)text andSuperView:(UIView *)superView
{
    if (![text isKindOfClass:[NSString class]]) {
        NSLog(@"请使用字符串对象");
        return;
    }
    if (text && text.length > 0) {
        NSMutableArray *textTempArr = [NSMutableArray new];
        [textTempArr addObject:text];
        
        if (textTempArr.count > 0) {
            [self drawAnimationWithTextArr:textTempArr andFontBgColor:_bgColor andFontColor:_fontColor andFontSize:_fontSize superView:superView];
        }
    }
}

#pragma mark -------------- 文字绘制(字符串数组)(使用默认属性) --------------
-(void)drawAnimationWithTextArr:(NSArray<NSString *> *)textArr andSuperView:(UIView *)superView
{
    if (![textArr isKindOfClass:[NSArray class]]) {
        NSLog(@"请使用数组对象");
        return;
    }
    if (textArr.count > 0) {
        [self drawAnimationWithTextArr:textArr andFontBgColor:_bgColor andFontColor:_fontColor andFontSize:_fontSize superView:superView];
    }
}

#pragma mark -------------- 文字绘制(字符串) --------------
-(void)drawAnimationWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    if (![text isKindOfClass:[NSString class]]) {
        NSLog(@"请使用字符串对象");
        return;
    }
    
    
//    _type = 100;
    
    if (text && text.length > 0) {
        NSMutableArray *textTempArr = [NSMutableArray new];
        [textTempArr addObject:text];
        
        if (textTempArr.count > 0) {
            [self drawAnimationWithTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFontSize:fontSize superView:superView];
        }
    }
}

#pragma mark -------------- 文字绘制(字符串数组) --------------
-(void)drawAnimationWithTextArr:(NSArray<NSString *> *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    if (![textArr isKindOfClass:[NSArray class]]) {
        NSLog(@"请使用数组对象");
        return;
    }
    CGFloat width = CGRectGetWidth(superView.frame);
    CGFloat height = CGRectGetHeight(superView.frame);
    
    if (width > 0) {
        _width = width;
    }
    
    if (height > 0) {
        _height = height;
    }
    
    _bgColor = bgColor ? bgColor :[UIColor clearColor];
    _fontColor = fontColor ? fontColor : [UIColor orangeColor];
    _fillColor = [UIColor clearColor];
    _fontSize = _fontSize > 0 ? _fontSize : (fontSize > 0 ? fontSize : 30);
    _font = _font ? _font : [UIFont systemFontOfSize:_fontSize];
    _mySuperView = superView;
    i = 0;
    _oneMoreText = YES;
    _fontHeight = _fontSize * 1.5;
    _lineNumber = _lineNumber > 0 ? _lineNumber : ( _width / (_fontSize * 1.3));
    
    if (_fontSize < 1) {
        NSLog(@"fontSize = %lf",_width);
        NSLog(@"请检测fontSize是不是配置有误");
        return;
    }
    
    if (_fontHeight < 1) {
        NSLog(@"_fontHeight = %lf",_fontHeight);
        NSLog(@"请检测_fontHeight是不是配置有误");
    }
    
    if (_lineNumber < 1) {
        NSLog(@"配置有误");
        return;
    }
    
    if (_width < 1) {
        NSLog(@"width = %lf",_width);
        NSLog(@"请检测width是不是配置有误");
    }
    
    if (_height < 1) {
        NSLog(@"height = %lf",_width);
        NSLog(@"请检测height是不是配置有误");
    }
    
    NSMutableArray *myTextArr = [NSMutableArray new];
    for (NSString *object in textArr) {
        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
        [myTextArr addObjectsFromArray:objectArr];
    }
    _textArr = [NSArray arrayWithArray:myTextArr];
    if (_textArr.count > i) {
        NSString *text = _textArr[i];
        [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:superView];
    }
}

#pragma mark -------------- 文字展示(字符串)(使用默认属性) --------------
-(void)showWithText:(NSString *)text superView:(UIView *)superView
{
    if (![text isKindOfClass:[NSString class]]) {
        NSLog(@"请使用字符串对象");
        return;
    }
    if (text && text.length > 0) {
        NSMutableArray *textTempArr = [NSMutableArray new];
        [textTempArr addObject:text];
        
        if (textTempArr.count > 0) {
            [self showTextArr:textTempArr andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize superView:superView];
        }
    }
}

#pragma mark -------------- 文字绘制(字符串数组)(使用默认属性) --------------
-(void)showWithTextArr:(NSArray<NSString *> *)textArr superView:(UIView *)superView
{
    if (![textArr isKindOfClass:[NSArray class]]) {
        NSLog(@"请使用数组对象");
        return;
    }
    if (textArr.count > 0) {
        [self showTextArr:textArr andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize superView:superView];
    }
}

#pragma mark -------------- 文字展示(字符串) --------------
-(void)showWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    if (![text isKindOfClass:[NSString class]]) {
        NSLog(@"请使用字符串对象");
        return;
    }
    
    if (text && text.length > 0) {
        NSMutableArray *textTempArr = [NSMutableArray new];
        [textTempArr addObject:text];
        
        if (textTempArr.count > 0) {
            [self showTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize superView:superView];
        }
    }
}

#pragma mark -------------- 文字展示(字符串数组) --------------
-(void)showTextArr:(NSArray<NSString *> *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    if (![textArr isKindOfClass:[NSArray class]]) {
        NSLog(@"请使用数组对象");
        return;
    }
    CGFloat width = CGRectGetWidth(superView.frame);
    CGFloat height = CGRectGetHeight(superView.frame);
    
    if (width > 0) {
        _width = width;
    }
    
    if (height > 0) {
        _height = height;
    }
    _bgColor = bgColor ? bgColor :[UIColor clearColor];
    _fontColor = fontColor ? fontColor : [UIColor orangeColor];
    _fillColor = fillColor ? fillColor : [UIColor clearColor];
    _fontSize = _fontSize > 0 ? _fontSize : (fontSize > 0 ? fontSize : 30);
    _font = _font ? _font : [UIFont systemFontOfSize:_fontSize];
    _mySuperView = superView;
    i = 0;
    _oneMoreText = YES;
    _fontHeight = _fontSize * 1.5;
    _lineNumber = _width / (_fontSize * 1.3);
    
    if (_fontSize < 1) {
        NSLog(@"fontSize = %lf",_width);
        NSLog(@"请检测fontSize是不是配置有误");
        return;
    }
    
    if (_fontHeight < 1) {
        NSLog(@"_fontHeight = %lf",_fontHeight);
        NSLog(@"请检测_fontHeight是不是配置有误");
    }
    
    if (_lineNumber < 1) {
        NSLog(@"配置有误");
        return;
    }
    
    if (_width < 1) {
        NSLog(@"width = %lf",_width);
        NSLog(@"请检测width是不是配置有误");
    }
    
    if (_height < 1) {
        NSLog(@"height = %lf",_width);
        NSLog(@"请检测height是不是配置有误");
    }
    
    NSMutableArray *myTextArr = [NSMutableArray new];
    for (NSString *object in textArr) {
        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
        [myTextArr addObjectsFromArray:objectArr];
    }
    _textArr = [NSArray arrayWithArray:myTextArr];
    
    for (NSString *object in _textArr) {
        [self showWithTextString:object andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:superView];
        i++;
    }
    if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)_mySuperView;
        scrollView.contentSize = CGSizeMake(_width, (i + 0) * _fontHeight);
        [self destroySelf];
    }
}

#pragma mark -------------- 文字绘制工作 --------------
-(void)drawAnimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andBounce:(CGRect)bounce superView:(UIView *)superView
{
    
//    CGFloat duration = 0.5;
//    NSInteger lenght = text.length;
//    if (lenght < 5) {
//        duration = 0.5;
//    }
//    else if (lenght < 10) {
//        duration = 1;
//    }
//    else if (lenght < 20)
//    {
//        duration = 2;
//    }
//    else if (lenght < 30)
//    {
//        duration = 3;
//    }
//    else
//    {
//        duration = 4;
//    }
    
    
    
    CGFloat multiple = 0;
    NSInteger number = text.length;
    
    
    
    if (number < 5) {
        multiple = 2.0;
    }
    else if (number < 10)
    {
        multiple = 1.9;
    }
    else if (number < 15)
    {
        multiple = 1.8;
    }
    else if (number < 20)
    {
        multiple = 1.5;
    }
    else
    {
        multiple = 1.5;
    }
    CGFloat duration = number * multiple;
    
    
    
    
    // Path
    UIBezierPath *path = [self bezierPathWithText:text attributes:@{NSFontAttributeName : _font}];
    
    
    // Layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = bounce;
    shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    shapeLayer.backgroundColor = [bgColor CGColor];
    shapeLayer.geometryFlipped = YES;
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = fontColor.CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = 2.0;
//    shapeLayer.lineJoin = kCALineJoinMiter;
    
    shapeLayer.lineJoin = kCALineJoinRound;
    [superView.layer addSublayer:shapeLayer];
    
       //文字、大小
//    shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//    shapeLayer.path = path.CGPath;
//    shapeLayer.strokeColor = fontColor.CGColor; // 字体颜色
    [shapeLayer removeAllAnimations];
    
    // Animation
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = duration; // 动画时间
    pathAnimation.fromValue = @(0.1);
    pathAnimation.toValue = @(1.0);
    pathAnimation.delegate = self;
    pathAnimation.speed = number;
    // Layer Add Animaiotion
    [shapeLayer addAnimation:pathAnimation forKey:@"path_animation"];
}

#pragma mark -------------- 文字显示工作 --------------
-(void)showWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
{
    UIBezierPath *path = [self bezierPathWithText:text attributes:@{NSFontAttributeName : _font}];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = bounce;
    shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    shapeLayer.backgroundColor = [bgColor CGColor];
    shapeLayer.geometryFlipped = YES;
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = fontColor.CGColor;
    shapeLayer.fillColor = fillColor.CGColor;
    shapeLayer.lineWidth = 2.0;
    shapeLayer.lineJoin = kCALineJoinRound;
    [superView.layer addSublayer:shapeLayer];
    [shapeLayer removeAllAnimations];
}

//-(void)drawAnimationWithText:(NSString *)text withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
//{
//    while (drawString.length > 0) {
//
//        NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
//        UIBezierPath *path = [self bezierPathForPortraitWithText:subString attributes:@{NSFontAttributeName : font}];
//        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//        shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//        NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
//        NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
//        _wordWidth = width > _wordWidth ? width : _wordWidth;
//        CGRect frame = CGRectMake(i * (_wordWidth + _lineSpace), currentY, width, height);
//        shapeLayer.frame = frame;
//        currentY += (height + _wordSpace);
//        if (currentY > _height) {
//            break;
//        }
//        else
//        {
//            shapeLayer.geometryFlipped = YES;
//            shapeLayer.path = path.CGPath;
//            shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
//            shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
//            shapeLayer.fillColor = [UIColor clearColor].CGColor;
//            shapeLayer.lineWidth = 2.0;
//            shapeLayer.lineJoin = kCALineJoinRound;
//            [superView.layer addSublayer:shapeLayer];
//            [shapeLayer removeAllAnimations];
//            shapeLayer.hidden = YES;
//            [_shapeLayers addObject:shapeLayer];
//            drawString = [drawString substringFromIndex:1];
//            NSLog(@"_height = %lf",_height);
//            NSLog(@"currentY = %ld",currentY);
//        }
//    }
//}

#pragma mark -------------- 字符串绘制（字符串） --------------
-(void)drawAnimationWithText:(NSString *)text withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
{
    
    [self resetData];
    
    drawString = text;
    _mySuperView = superView;
    _type = type;
    _fontColor = fontColor;
    _font = font;
    
    _width = CGRectGetWidth(superView.bounds);
    _height = CGRectGetHeight(superView.bounds);
    
    [self drawAnimationWithTextString:text withType:type fontColor:fontColor font:font superView:superView];
    
    
}

#pragma mark -------------- 字符串绘制（字符串数组） --------------
-(void)drawAnimationWithTextArr:(NSArray *)textArr withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
{
//    _stringIndex = 0;
    if (textArr.count > 0) {
        _textArr = textArr;
        NSString *subString = textArr[_stringIndex];
        [self drawAnimationWithTextString:subString withType:type fontColor:fontColor font:font superView:superView];
    }
}

#pragma mark -------------- 文字展示工作 --------------
-(void)showWithText:(NSString *)text withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
{
    [self resetData];
    
    drawString = text;
    _mySuperView = superView;
    _type = type;
    _fontColor = fontColor;
    _font = font;
    
    _width = CGRectGetWidth(superView.bounds);
    _height = CGRectGetHeight(superView.bounds);
    
    
    switch (type) {
        case TextAnimationTypePortrait:
        {//纵向排版
//            _shapeLayers = [NSMutableArray new];
            subIndex = 0;
            
            static NSInteger currentY;
            currentY = 0;
            
            while (drawString.length > 0) {
                
                NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                UIBezierPath *path = [self bezierPathForPortraitWithText:subString attributes:@{NSFontAttributeName : font}];
                CAShapeLayer *shapeLayer = [CAShapeLayer layer];
                shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
                NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
                NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
                _wordWidth = width > _wordWidth ? width : _wordWidth;
                CGRect frame = CGRectMake(i * (_wordWidth + _lineSpace), currentY, width, height);
                shapeLayer.frame = frame;
                currentY += (height + _wordSpace);
                if (currentY > _height) {
                    if (drawString.length > 0) {
                        i++;
                        currentY = 0;
                    }
                    else
                    {
                        break;
                    }
                    
                }
                else
                {
                    shapeLayer.geometryFlipped = YES;
                    shapeLayer.path = path.CGPath;
                    shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
                    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    shapeLayer.lineJoin = kCALineJoinRound;
                    [superView.layer addSublayer:shapeLayer];
                    [shapeLayer removeAllAnimations];
//                    shapeLayer.hidden = YES;
//                    [_shapeLayers addObject:shapeLayer];
                    drawString = [drawString substringFromIndex:1];
                    NSLog(@"_height = %lf",_height);
                    NSLog(@"currentY = %ld",currentY);
                }
            }
            
            _stringIndex++;
            if (_textArr.count > 0 && _stringIndex < _textArr.count) {
                currentY = 0;
                i++;
                NSString *subString = _textArr [_stringIndex];
                [self showWithText:subString withType:type fontColor:fontColor font:font superView:superView];
            }
            else
            {
                NSLog(@"文字展示工作完成");
                
                [self destroySelf];
            }
            
            
            if ([superView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)superView;
                if ((i + 1) * (_wordWidth + _lineSpace) > _width) {
                    scrollView.contentSize = CGSizeMake((i + 1) * (_wordWidth + _lineSpace), _height);
                }
            }
            
            
            
        }
            break;
        case TextAnimationTypeTransverse:
        {//横向排版
            
//            _shapeLayers = [NSMutableArray new];
            subIndex = 0;
            
            static NSInteger currentX;
            currentX = 0;
            
            while (drawString.length > 0) {
                
                NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                UIBezierPath *path = [self bezierPathWithText:subString attributes:@{NSFontAttributeName : font}];
                CAShapeLayer *shapeLayer = [CAShapeLayer layer];
                shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
                NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
                NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
                //                _wordWidth = (width > _wordWidth) ? width : _wordWidth;
                _lineHeight = (height > _lineHeight) ? height : _lineHeight;
                //                _lineHeight +=
                CGRect frame = CGRectMake(currentX, i * (_lineHeight + _lineSpace), width, height);
                shapeLayer.frame = frame;
                currentX += (width + _wordSpace);
                if (currentX > _width) {
                    NSLog(@"超出屏幕宽度了");
                    NSLog(@"_width = %lf",_width);
                    NSLog(@"currentX = %ld",currentX);
                    
                    if (drawString.length > 0) {
                        i++;
                        currentX = 0;
                    }
                    else
                    {
                        NSLog(@"方法需要被终止");
                        break;
                    }
                    
                }
                else
                {
                    shapeLayer.geometryFlipped = YES;
                    shapeLayer.path = path.CGPath;
                    shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
                    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    shapeLayer.lineJoin = kCALineJoinRound;
                    [superView.layer addSublayer:shapeLayer];
                    [shapeLayer removeAllAnimations];
//                    shapeLayer.hidden = YES;
//                    [_shapeLayers addObject:shapeLayer];
                    drawString = [drawString substringFromIndex:1];
                    NSLog(@"_width = %lf",_width);
                    NSLog(@"currentX = %ld",currentX);
                }
            }
            
            _stringIndex++;
            if (_textArr.count > 0 && _stringIndex < _textArr.count) {
                currentX = 0;
                i++;
                NSString *subString = _textArr [_stringIndex];
                [self showWithText:subString withType:type fontColor:fontColor font:font superView:superView];
            }
            else
            {
                NSLog(@"文字展示工作完成");
                
                [self destroySelf];
            }
            
            if ([superView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)superView;
                if ((i + 1) * (_lineHeight + _lineSpace) > _height) {
                    scrollView.contentSize = CGSizeMake(_width, (i + 1) * (_lineHeight + _lineSpace));
                }
            }
        }
            break;
            
        default:
            break;
    }
//    if (_shapeLayers.count > 0) {
//        CAShapeLayer *layer = [_shapeLayers firstObject];
//        layer.hidden = NO;
//        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//        pathAnimation.duration = 1;
//        pathAnimation.fromValue = @(0.1);
//        pathAnimation.toValue = @(1.0);
//        pathAnimation.delegate = self;
//        [layer addAnimation:pathAnimation forKey:@"path_animation"];
//    }
}
#pragma mark -------------- 文字展示（字符串数组） --------------
-(void)showWithTextArr:(NSArray *)textArr withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
{
    if (textArr.count > 0) {
        _textArr = textArr;
        _stringIndex = 0;
        NSString *subString = [_textArr firstObject];
        [self showWithText:subString withType:type fontColor:fontColor font:font superView:superView];
    }
}
#pragma mark -------------- 文字绘制方法 --------------
-(void)drawAnimationWithTextString:(NSString *)text withType:(TextAnimationType)type fontColor:(UIColor *)fontColor font:(UIFont *)font superView:(UIView *)superView
{
    [self resetData];
    
    drawString = text;
    _mySuperView = superView;
    _type = type;
    _fontColor = fontColor;
    _font = font;
    
    _width = CGRectGetWidth(superView.bounds);
    _height = CGRectGetHeight(superView.bounds);
    
    
    switch (type) {
        case TextAnimationTypePortrait:
        {//纵向排版
            
            
            
            /*
            
            //如果需要让相连字母或者相连数字横向输出，则使用下面被注掉的方法，然后将下面正在使用的方法注释掉
            
            
            _shapeLayers = [NSMutableArray new];
            subIndex = 0;
//            _wordWidth = 0;
            static NSInteger currentY;
            currentY = 0;
            
//            NSMutableArray *ranges = [NSMutableArray new];
//
//            NSString *letterParten =@"[a-zA-Z]+";//从字符串中找出相连的字母
//            NSString *numberParten = @"[0-9]+";//从字符串中找出相连的数字
//            NSError *error = nil;
//            NSRegularExpression *letterRegular = [NSRegularExpression regularExpressionWithPattern:letterParten options:NSRegularExpressionCaseInsensitive error:&error]; //options 根据自己需求选择
//
//            NSArray *letterMatches = [letterRegular matchesInString:drawString options:NSMatchingReportCompletion range:NSMakeRange(0, drawString.length)];
//            for (NSTextCheckingResult *match in letterMatches) {
//                //NSRange matchRange = [match range]; //获取所匹配的最长字符串
//                for (int i = 0; i < match.numberOfRanges; i++) {
//                    NSRange matchRange = [match rangeAtIndex:i];
//                    NSString *matchString = [drawString substringWithRange:matchRange];
//                    NSLog(@"index:%@, %@", @(i), matchString);
//                    [ranges addObject:[NSValue valueWithRange:matchRange]];
//                }
//            }
//
//            NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:numberParten options:NSRegularExpressionCaseInsensitive error:&error]; //options 根据自己需求选择
//
//            NSArray *numberMatches = [numberRegular matchesInString:drawString options:NSMatchingReportCompletion range:NSMakeRange(0, drawString.length)];
//            for (NSTextCheckingResult *match in numberMatches) {
//                //NSRange matchRange = [match range]; //获取所匹配的最长字符串
//                for (int i = 0; i < match.numberOfRanges; i++) {
//                    NSRange matchRange = [match rangeAtIndex:i];
//                    NSString *matchString = [drawString substringWithRange:matchRange];
//                    NSLog(@"index:%@, %@", @(i), matchString);
//                    [ranges addObject:[NSValue valueWithRange:matchRange]];
//                }
//            }
            
//            NSInteger deleteNumber = 0;
            while (drawString.length > 0) {
//                NSLog(@"deleteNumber = %ld",deleteNumber);
                NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
//                NSInteger latterIndex = 0;
                NSMutableString *mutableString = [NSMutableString new];
                
                if (((([subString compare:@"0"] == NSOrderedDescending) || ([subString compare:@"0"] == NSOrderedSame)) && (([subString compare:@"9"] == NSOrderedAscending) || ([subString compare:@"9"] == NSOrderedSame)))) {
                    while (((([subString compare:@"0"] == NSOrderedDescending) || ([subString compare:@"0"] == NSOrderedSame)) && (([subString compare:@"9"] == NSOrderedAscending) || ([subString compare:@"9"] == NSOrderedSame)))) {
                        NSLog(@"是数字：%@",subString);
                        [mutableString appendString:subString];
                        if (drawString.length > 0) {
                            drawString = [drawString substringFromIndex:1];
                            if (drawString.length > 0) {
                                subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                            }
                            else
                            {
                                subString = @"";
                                break;
                            }
                        }
                        
                    }
                }
                else
                {
                    while (((([subString compare:@"a"] == NSOrderedDescending) || ([subString compare:@"a"] == NSOrderedSame)) && (([subString compare:@"z"] == NSOrderedAscending) || ([subString compare:@"z"] == NSOrderedSame))) || ((([subString compare:@"A"] == NSOrderedDescending) || ([subString compare:@"A"] == NSOrderedSame)) && (([subString compare:@"Z"] == NSOrderedAscending) || ([subString compare:@"Z"] == NSOrderedSame)))) {
                        NSLog(@"是字母：%@",subString);
                        [mutableString appendString:subString];
                        if (drawString.length > 0) {
                            drawString = [drawString substringFromIndex:1];
                            if (drawString.length > 0) {
                                subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                            }
                            else
                            {
                                subString = @"";
                                break;
                            }
                        }
                        
                    }
                }
                
                UIBezierPath *path;
                if (mutableString.length > 0) {
                    path = [self bezierPathWithText:mutableString attributes:@{NSFontAttributeName : font}];
                }
                else
                {
                    path = [self bezierPathForPortraitWithText:subString attributes:@{NSFontAttributeName : font}];
                }
                
                
//                UIBezierPath *path = [self bezierPathForPortraitWithText:subString attributes:@{NSFontAttributeName : font}];
                CAShapeLayer *shapeLayer = [CAShapeLayer layer];
                shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
                NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
                NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
                CGRect frame = CGRectMake(i * (_wordWidth + _lineSpace), currentY, width, height);
                _wordWidth = width > _wordWidth ? width : _wordWidth;
                shapeLayer.frame = frame;
                currentY += (height + _wordSpace);
                if (currentY > _height) {
                    break;
                }
                else
                {
                    shapeLayer.geometryFlipped = YES;
                    shapeLayer.path = path.CGPath;
                    shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
                    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    shapeLayer.lineJoin = kCALineJoinRound;
                    [superView.layer addSublayer:shapeLayer];
                    [shapeLayer removeAllAnimations];
                    shapeLayer.hidden = YES;
                    [_shapeLayers addObject:shapeLayer];
                    if (drawString.length > 0 && mutableString.length == 0) {
                        drawString = [drawString substringFromIndex:1];
                    }
                    
//                    deleteNumber++;
                    NSLog(@"_height = %lf",_height);
                    NSLog(@"currentY = %ld",currentY);
                }
            }
             
             
             
             
             
             
             */
            
            
            
            
            
            
            
            
            
            
            
            
            _shapeLayers = [NSMutableArray new];
            subIndex = 0;
            static NSInteger currentY;
            currentY = 0;
            while (drawString.length > 0) {
                NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                UIBezierPath *path = [self bezierPathForPortraitWithText:subString attributes:@{NSFontAttributeName : font}];
                CAShapeLayer *shapeLayer = [CAShapeLayer layer];
                shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
                NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
                NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
                CGRect frame = CGRectMake(i * (_wordWidth + _lineSpace), currentY, width, height);
                _wordWidth = width > _wordWidth ? width : _wordWidth;
                shapeLayer.frame = frame;
                currentY += (height + _wordSpace);
                if (currentY > _height) {
                    break;
                }
                else
                {
                    shapeLayer.geometryFlipped = YES;
                    shapeLayer.path = path.CGPath;
                    shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
                    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    shapeLayer.lineJoin = kCALineJoinRound;
                    [superView.layer addSublayer:shapeLayer];
                    [shapeLayer removeAllAnimations];
                    shapeLayer.hidden = YES;
                    [_shapeLayers addObject:shapeLayer];
                    if (drawString.length > 0) {
                        drawString = [drawString substringFromIndex:1];
                    }
                    NSLog(@"_height = %lf",_height);
                    NSLog(@"currentY = %ld",currentY);
                }
            }
            
            
            
            
            
            
        }
            break;
        case TextAnimationTypeTransverse:
        {//横向排版
            
            _shapeLayers = [NSMutableArray new];
            subIndex = 0;
            
            static NSInteger currentX;
            currentX = 0;
            
            while (drawString.length > 0) {
                
                NSString *subString = [drawString substringWithRange:NSMakeRange(0, 1)];
                UIBezierPath *path = [self bezierPathWithText:subString attributes:@{NSFontAttributeName : font}];
                CAShapeLayer *shapeLayer = [CAShapeLayer layer];
                shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);
                NSInteger width = floorf(CGRectGetWidth(shapeLayer.bounds));
                NSInteger height = floorf(CGRectGetHeight(shapeLayer.bounds));
//                _wordWidth = (width > _wordWidth) ? width : _wordWidth;
                _lineHeight = (height > _lineHeight) ? height : _lineHeight;
//                _lineHeight +=
                CGRect frame = CGRectMake(currentX, i * (_lineHeight + _lineSpace), width, height);
                shapeLayer.frame = frame;
                currentX += (width + _wordSpace);
                if (currentX > _width) {
                    NSLog(@"超出屏幕宽度了");
                    NSLog(@"_width = %lf",_width);
                    NSLog(@"currentX = %ld",currentX);
                    break;
                }
                else
                {
                    shapeLayer.geometryFlipped = YES;
                    shapeLayer.path = path.CGPath;
                    shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
                    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
                    shapeLayer.fillColor = [UIColor clearColor].CGColor;
                    shapeLayer.lineWidth = 2.0;
                    shapeLayer.lineJoin = kCALineJoinRound;
                    [superView.layer addSublayer:shapeLayer];
                    [shapeLayer removeAllAnimations];
                    shapeLayer.hidden = YES;
                    [_shapeLayers addObject:shapeLayer];
                    drawString = [drawString substringFromIndex:1];
                    NSLog(@"_width = %lf",_width);
                    NSLog(@"currentX = %ld",currentX);
                }
            }
        }
            break;
            
        default:
            break;
    }
    if (_shapeLayers.count > 0) {
        CAShapeLayer *layer = [_shapeLayers firstObject];
        layer.hidden = NO;
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 1;
        pathAnimation.fromValue = @(0.1);
        pathAnimation.toValue = @(1.0);
        pathAnimation.delegate = self;
        [layer addAnimation:pathAnimation forKey:@"path_animation"];
    }
}

#pragma mark -------------- CAAnimationDelegate --------------
- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"开始动画");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//    NSLog(@"动画结束");
    
    __weak typeof(self) weakSelf = self;
    
    
    
    
    if (_oneMoreText) {
        //        static NSInteger i = 1;
        i++;
        if (i < _textArr.count) {
            if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                if (scrollView.contentSize.height > _height) {
                    [UIView animateWithDuration:0.5f animations:^{
                        //                        NSLog(@"currentThread = %@",[NSThread currentThread]);
                        scrollView.contentOffset = CGPointMake(0, (i + 1) * weakSelf.fontHeight - weakSelf.height);
                    } completion:^(BOOL finished) {
                        
                        NSString *text = weakSelf.textArr[i];
                        [weakSelf drawAnimationWithTextString:text andFontBgColor:weakSelf.bgColor andFontColor:weakSelf.fontColor andBounce:CGRectMake(0, i * weakSelf.fontHeight, weakSelf.width, weakSelf.fontHeight) superView:weakSelf.mySuperView];
                    }];
                }
                else
                {
                    NSString *text = _textArr[i];
                    [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:_mySuperView];
                }
            }
            else
            {
                NSString *text = _textArr[i];
                [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:_mySuperView];
            }
        }
        else
        {
            NSLog(@"动画结束");
            
            [self destroySelf];
            return;
        }
        return;
    }
//    else
//    {
////        NSLog(@"动画结束");
////        NSLog(@"此处永远也不会执行");
//        
//    }
    
    
    
    
    
    switch (_type)
    {
        case TextAnimationTypePortrait:
        {//纵向排版
            subIndex++;
            
            if (subIndex < _shapeLayers.count) {
                CAShapeLayer *layer = _shapeLayers [subIndex];
                layer.hidden = NO;
                // Animation
                CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
                //    pathAnimation.duration = duration; // 动画时间
                pathAnimation.duration = 1;
                pathAnimation.fromValue = @(0.1);
                pathAnimation.toValue = @(1.0);
                pathAnimation.delegate = self;
                //    pathAnimation.speed = number;
                // Layer Add Animaiotion
                [layer addAnimation:pathAnimation forKey:@"path_animation"];
            }
            else
            {
                subIndex = 0;
                [_shapeLayers removeAllObjects];
                
                if (drawString.length > 0) {
                    i++;
                    
                    if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                        UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                        scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                        if ((i + 1) * (_wordWidth + _lineSpace) > _width) {
                            [UIView animateWithDuration:0.5f animations:^{
                                scrollView.contentOffset = CGPointMake((i + 1) * (weakSelf.wordWidth + weakSelf.lineSpace) - weakSelf.width, 0);
                            } completion:^(BOOL finished) {
                                
                                [self drawAnimationWithText:drawString withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                            }];
                        }
                        else
                        {
                            [self drawAnimationWithText:drawString withType:_type fontColor:_fontColor font:_font superView:_mySuperView];
                        }
                    }
                    else
                    {
                        [self drawAnimationWithText:drawString withType:_type fontColor:_fontColor font:_font superView:_mySuperView];
                    }
                }
                else
                {
                    _stringIndex++;
                    i++;
                    
                    if (_stringIndex < _textArr.count) {
                        
                        
                        
                        if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                            UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                            scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                            if ((i + 1) * (_wordWidth + _lineSpace) > _width) {
                                [UIView animateWithDuration:0.5f animations:^{
                                    scrollView.contentOffset = CGPointMake((i + 1) * (weakSelf.wordWidth + weakSelf.lineSpace) - weakSelf.width, 0);
                                } completion:^(BOOL finished) {
                                    
                                    [self drawAnimationWithTextArr:weakSelf.textArr withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                                }];
                            }
                            else
                            {
                                [self drawAnimationWithTextArr:_textArr withType:_type fontColor:_fontColor font:_font superView:_mySuperView];
                            }
                        }
                        else
                        {
                            [self drawAnimationWithTextArr:_textArr withType:_type fontColor:_fontColor font:_font superView:_mySuperView];
                        }
                    }
                    
                    
                    else if ((i + 1) * (_wordWidth + _lineSpace) > _width) {
                        NSLog(@"绘制完成");
                        if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                            UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                            scrollView.contentSize = CGSizeMake((i) * (_wordWidth + _lineSpace), _height);
                        }
                        
                        [self destroySelf];
                    }
                    
                }
            }
            
            
        }
            break;
            
        case TextAnimationTypeTransverse:
        {//横向排版
            subIndex++;
            
            if (subIndex < _shapeLayers.count) {
                CAShapeLayer *layer = _shapeLayers [subIndex];
                layer.hidden = NO;
                // Animation
                CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
                //    pathAnimation.duration = duration; // 动画时间
                pathAnimation.duration = 1;
                pathAnimation.fromValue = @(0.1);
                pathAnimation.toValue = @(1.0);
                pathAnimation.delegate = self;
                //    pathAnimation.speed = number;
                // Layer Add Animaiotion
                [layer addAnimation:pathAnimation forKey:@"path_animation"];
            }
            else
            {
                if (drawString.length > 0) {
                    if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                        UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                        scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                        if ((i + 2) * (_lineHeight + _lineSpace) > _height) {
                            [UIView animateWithDuration:0.5f animations:^{
                                scrollView.contentOffset = CGPointMake(0, (i + 2) * (weakSelf.lineHeight + weakSelf.lineSpace) - weakSelf.height);
                            } completion:^(BOOL finished) {
                                i++;
                                [self drawAnimationWithText:drawString withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                            }];
                        }
                        else
                        {
                            i++;
                            [self drawAnimationWithText:drawString withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                        }
                    }
                    else
                    {
                        i++;
                        [self drawAnimationWithText:drawString withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                    }
                }
                else
                {
                    _stringIndex++;
//                    i++;
                    if (_stringIndex < _textArr.count) {
                        if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                            UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                            scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                            if ((i + 2) * (_lineHeight + _lineSpace) > _height) {
                                [UIView animateWithDuration:0.5f animations:^{
                                    scrollView.contentOffset = CGPointMake(0, (i + 2) * (weakSelf.lineHeight + weakSelf.lineSpace) - weakSelf.height);
                                } completion:^(BOOL finished) {
                                    i++;
                                    [self drawAnimationWithTextArr:weakSelf.textArr withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                                }];
                            }
                            else
                            {
                                i++;
                                [self drawAnimationWithTextArr:_textArr withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                            }
                        }
                        else
                        {
                            i++;
                            [self drawAnimationWithTextArr:_textArr withType:weakSelf.type fontColor:weakSelf.fontColor font:weakSelf.font superView:weakSelf.mySuperView];
                        }
                    }
                    else
                    {
                        NSLog(@"绘制完成");
                        if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                            UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                            scrollView.contentSize = CGSizeMake(_width, (i + 1) * (weakSelf.lineHeight + weakSelf.lineSpace));
                        }
                        
                        [self destroySelf];
                    }
                    
                    
                    
                    
                }
            }
                
            
        }
            break;
        default:
            break;
    }
    
    
    
//    return;
}

#pragma mark -------------- 横向排版 --------------
- (UIBezierPath *)bezierPathWithText:(NSString *)text attributes:(NSDictionary *)attrs; {
    NSAssert(text!= nil && attrs != nil, @"参数不能为空");
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrs];
    CGMutablePathRef paths = CGPathCreateMutable();
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
    CFArrayRef runArray = CTLineGetGlyphRuns(line);
    
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
    {
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
        
        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
        {
            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunGetPositions(run, thisGlyphRange, &position);
            {
                CGPathRef path = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
                CGPathAddPath(paths, &t,path);
                CGPathRelease(path);
            }
        }
    }
    CFRelease(line);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:paths]];
    CGPathRelease(paths);
    return path;
}

#pragma mark -------------- 纵向排版 --------------
- (UIBezierPath *)bezierPathForPortraitWithText:(NSString *)text attributes:(NSDictionary *)attrs; {
    NSAssert(text!= nil && attrs != nil, @"参数不能为空");
    text = [self inputValue:text];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrs];
    CGMutablePathRef paths = CGPathCreateMutable();
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
    CFArrayRef runArray = CTLineGetGlyphRuns(line);
    
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
    {
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
        
        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
        {
            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunGetPositions(run, thisGlyphRange, &position);
            {
                CGPathRef path = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.y, position.x);
                //                NSLog(@"position = %@",NSStringFromCGPoint(position));
                CGPathAddPath(paths, &t,path);
                CGPathRelease(path);
            }
        }
    }
    CFRelease(line);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:paths]];
    CGPathRelease(paths);
    return path;
}

//完全逆序 输入“1234” 输出“4321”
#pragma mark -------------- 字符串逆序 --------------
-(NSString *)inputValue:(NSString *)str
{
    NSMutableString *string=[[NSMutableString alloc] init];
    for(int i=0;i<str.length;i++){
        [string appendString:[str substringWithRange:NSMakeRange(str.length-i-1, 1)]];
    }
    return string;
}

-(NSArray *)arrangementText:(NSString *)text withLength:(NSInteger)length
{
    NSMutableArray *textTempArr = [NSMutableArray new];
    NSString *textString = text;
    while (textString.length > length) {
        NSString *currentString = [textString substringToIndex:length];
        textString = [textString substringFromIndex:length];
        [textTempArr addObject:currentString];
    }
    if (textString.length > 0) {
        [textTempArr addObject:textString];
    }
    return textTempArr;
}

-(void)setSuperViewWidth:(CGFloat)superViewWidth
{
    _superViewWidth = superViewWidth;
    _width = superViewWidth;
}

-(void)setSuperViewHeight:(CGFloat)superViewHeight
{
    _superViewHeight = superViewHeight;
    _height = superViewHeight;
}

-(void)setSuperViewSize:(CGSize)superViewSize
{
    _superViewSize = superViewSize;
    self.superViewWidth = superViewSize.width;
    self.superViewHeight = superViewSize.height;
}
#pragma mark -------------- 销毁工具 --------------
-(void)destroySelf
{
    NSLog(@"销毁工具中的对象");
    
    _stringIndex = 0;
    i = 0;
    drawString = @"";
    subIndex = 0;
    
    
//    _oneMoreText = NO;
//    _textArr = nil;
//    _width = 0;
//    _wordWidth = 0;
//    _lineHeight = 0;
//    _height = 0;
//    _mySuperView = nil;
//    _fontHeight = 0;
//
//    _lineNumber = 0;
    
    
    
    if (self.DrawAnimationFinishedBlock) {
        self.DrawAnimationFinishedBlock();
    }
//    singletonInstance = nil;
//    onceToken = 0;
}
#pragma mark -------------- 默认属性重置 --------------
-(void)resetData
{
    _wordSpace = _wordSpace > 0 ? _wordSpace : 10;
    _lineSpace = _lineSpace > 0 ? _lineSpace : 30;
}

-(void)dealloc
{
    NSLog(@"销毁文字动画绘制工具");
}



@end
